package com.central.task.controller;


import com.central.common.model.UdfDefined;
import com.central.task.service.IUdfDefinedService;
import com.central.common.annotation.LoginUser;
import com.central.common.annotation.RequiredPermission;
import com.central.common.constant.PermissionConstants;
import com.central.common.model.Result;
import com.central.common.model.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author Tindy
 * @date 2021/11/25
 * @describe
 */
@Slf4j
@RestController
@Api(tags = "udf函数定义模块api")
@RequestMapping(value = "/udfDefined")
public class UdfDefinedController {


    @Autowired
    private IUdfDefinedService udfDefinedService;

    @ApiOperation(value = "创建或更新函数")
    @PostMapping("/saveOrUpdateUdf")
    @RequiredPermission({PermissionConstants.DEV_USER,PermissionConstants.PRO_ADMIN})
    public Result saveOrUpdateUdf(@ApiIgnore @LoginUser SysUser user, @RequestBody UdfDefined udf){
        return udfDefinedService.saveOrUpdateUdf(user,udf);
    }

    @ApiOperation(value = "udf列表")
    @GetMapping("/findUdfs")
    public List<UdfDefined> findUdfs(@ApiIgnore @LoginUser SysUser user) {
        return udfDefinedService.findUdfs(user.getCurrentProjectId());
    }

    @ApiOperation(value = "删除udf")
    @DeleteMapping("/delete")
    @RequiredPermission({PermissionConstants.DEV_USER,PermissionConstants.PRO_ADMIN})
    public Result delete(@ApiIgnore @LoginUser SysUser user,Long id){
        try {
            return udfDefinedService.delete(user,id);
        }catch (Exception e){
            return Result.failed("操作失败："+e.getMessage());
        }
    }

    @ApiOperation(value = "通过ids查询udf")
    @PostMapping("/findUdfsByIds")
    public List<UdfDefined> findUdfsByIds(@RequestBody List<Long> ids) {
        return udfDefinedService.findUdfsByIds(ids);
    }
}
